<?php
/**
 * Class that operate on table 'sic_transaccion'. Database Mysql.
 *
 * @author: http://phpdao.com
 * @date: 2014-10-07 23:31
 */
class SicTransaccionMySqlDAO implements SicTransaccionDAO{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return SicTransaccionMySql 
	 */
	public function load($id){
		$sql = 'SELECT * FROM sic_transaccion WHERE id_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll(){
		$sql = 'SELECT * FROM sic_transaccion';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn){
		$sql = 'SELECT * FROM sic_transaccion ORDER BY '.$orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
 	 * Delete record from table
 	 * @param sicTransaccion primary key
 	 */
	public function delete($id_transaccion){
		$sql = 'DELETE FROM sic_transaccion WHERE id_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($id_transaccion);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
 	 * Insert record to table
 	 *
 	 * @param SicTransaccionMySql sicTransaccion
 	 */
	public function insert($sicTransaccion){
		$sql = 'INSERT INTO sic_transaccion (concepto_transaccion, fecha_transaccion, importe_transaccion, ajuste_transaccion) VALUES (?, ?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sicTransaccion->conceptoTransaccion);
		$sqlQuery->set($sicTransaccion->fechaTransaccion);
		$sqlQuery->set($sicTransaccion->importeTransaccion);
		$sqlQuery->setNumber($sicTransaccion->ajusteTransaccion);

		$id = $this->executeInsert($sqlQuery);	
		$sicTransaccion->idTransaccion = $id;
		return $id;
	}
	
	/**
 	 * Update record in table
 	 *
 	 * @param SicTransaccionMySql sicTransaccion
 	 */
	public function update($sicTransaccion){
		$sql = 'UPDATE sic_transaccion SET concepto_transaccion = ?, fecha_transaccion = ?, importe_transaccion = ?, ajuste_transaccion = ? WHERE id_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($sicTransaccion->conceptoTransaccion);
		$sqlQuery->set($sicTransaccion->fechaTransaccion);
		$sqlQuery->set($sicTransaccion->importeTransaccion);
		$sqlQuery->setNumber($sicTransaccion->ajusteTransaccion);

		$sqlQuery->setNumber($sicTransaccion->idTransaccion);
		return $this->executeUpdate($sqlQuery);
	}

	/**
 	 * Delete all rows
 	 */
	public function clean(){
		$sql = 'DELETE FROM sic_transaccion';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByConceptoTransaccion($value){
		$sql = 'SELECT * FROM sic_transaccion WHERE concepto_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByFechaTransaccion($value){
		$sql = 'SELECT * FROM sic_transaccion WHERE fecha_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByImporteTransaccion($value){
		$sql = 'SELECT * FROM sic_transaccion WHERE importe_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByAjusteTransaccion($value){
		$sql = 'SELECT * FROM sic_transaccion WHERE ajuste_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByConceptoTransaccion($value){
		$sql = 'DELETE FROM sic_transaccion WHERE concepto_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByFechaTransaccion($value){
		$sql = 'DELETE FROM sic_transaccion WHERE fecha_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByImporteTransaccion($value){
		$sql = 'DELETE FROM sic_transaccion WHERE importe_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByAjusteTransaccion($value){
		$sql = 'DELETE FROM sic_transaccion WHERE ajuste_transaccion = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->setNumber($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return SicTransaccionMySql 
	 */
	protected function readRow($row){
		$sicTransaccion = new SicTransaccion();
		
		$sicTransaccion->idTransaccion = $row['id_transaccion'];
		$sicTransaccion->conceptoTransaccion = $row['concepto_transaccion'];
		$sicTransaccion->fechaTransaccion = $row['fecha_transaccion'];
		$sicTransaccion->importeTransaccion = $row['importe_transaccion'];
		$sicTransaccion->ajusteTransaccion = $row['ajuste_transaccion'];

		return $sicTransaccion;
	}
	
	protected function getList($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++){
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return SicTransaccionMySql 
	 */
	protected function getRow($sqlQuery){
		$tab = QueryExecutor::execute($sqlQuery);
		if(count($tab)==0){
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery){
		return QueryExecutor::execute($sqlQuery);
	}
	
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery){
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery){
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery){
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>